<?php

namespace app\admin\traits;

use app\common\exception\ApiErrDesc;
use app\common\exception\ApiException;
use think\facade\Db;


/**
 * 后台Admin权限验证
 * Trait EndAuthTrait
 * @package app\common\traits
 */
trait EndAuthTrait
{
    /**
     * ^2_3^检测权限
     * @param string $rule
     * @param int $adminId
     * @return bool
     * @throws ApiException
     * @author ^2_3^王尔贝
     */
    public function authCheck($rule = '', $adminId = 0)
    {
        if( empty( $adminId ) ) {
            return false;
        }

        //// 是否超管
        $adminRoles = Db::name('admin_role')
            ->where(['admin_id' => $adminId])
            ->column('role_id');
        if (!empty( $adminRoles ) && in_array(1, $adminRoles)) {
            return true;

        }else if( empty( $adminRoles ) ) {
            return false;
        }

        //// 路由验证
        if( empty($rule) ) {
            $rule = request()->rule()->getRoute();
            preg_match_all('/app\\\(\w+)\\\controller\\\(\w+)@(\w+)/', $rule, $ruleMatches);
            if( count( $ruleMatches ) != 4 ) {
                throw new ApiException(ApiErrDesc::END_RULE_PARSE);
            }
            $module = app('http')->getName();
            $controller = $ruleMatches['2'][0];
            $action = $ruleMatches['3'][0];

        }else{
            $rule = explode('/', $rule);
            if( count($rule)==3 ) {
                $module = $rule[0];
                $controller = $rule[1];
                $action = $rule[2];

            }elseif( count($rule)==2 ) {
                $module = 'admin';
                $controller = $rule[0];
                $action = $rule[1];

            }else{
                $rule = request()->rule()->getRoute();
                preg_match_all('/app\\\(\w+)\\\controller\\\(\w+)@(\w+)/', $rule, $ruleMatches);
                if( count( $ruleMatches ) != 4 ) {
                    throw new ApiException(ApiErrDesc::END_RULE_PARSE);
                }

                $module = 'admin';
                $controller = $ruleMatches['2'][0];
                $action = $rule[0];
            }
        }
        //
        $where = [];
        $where['app'] = $module;
        $where['controller'] = $controller;
        $where['action'] = $action;
        if( !empty( $adminRoles ) ) {
            $where['role_id'] = ['in', implode(',', $adminRoles)];
        }
        $res = Db::name('menu_node')
            ->alias('node')
            ->field('node.id')
            ->join('role_node rn', 'node.id = rn.node_id')
            ->where($where)
            ->find();
        if( $res ) {
            return true;

        }else{
            return false;
        }
    }

    /**
     * 路由验证(session)
     * @param string $rule
     * @return bool
     * @throws ApiException
     * @author ^2_3^王尔贝
     */
    public function authCheckBySession($rule = '')
    {
        //// 是否超管
        $sessionAdminId = session('ADMIN_ID');
        //
        $adminRoles = Db::name('admin_role')
            ->where(['admin_id' => $sessionAdminId])
            ->column('role_id');
        if (!empty( $adminRoles ) && in_array(1, $adminRoles)) {
            return true;

        }else if( empty( $adminRoles ) ) {
            return false;
        }

        //// 路由验证
        if( empty($rule) ) {
            $rule = request()->rule()->getRoute();
            preg_match_all('/app\\\(\w+)\\\controller\\\(\w+)@(\w+)/', $rule, $ruleMatches);
            if( count( $ruleMatches ) != 4 ) {
                throw new ApiException(ApiErrDesc::END_RULE_PARSE);
            }
            $module = app('http')->getName();
            $controller = $ruleMatches['2'][0];
            $action = $ruleMatches['3'][0];

        }else{
            $rule = explode('/', $rule);
            if( count($rule)==3 ) {
                $module = $rule[0];
                $controller = $rule[1];
                $action = $rule[2];

            }elseif( count($rule)==2 ) {
                $module = 'admin';
                $controller = $rule[0];
                $action = $rule[1];

            }else{
                $rule = request()->rule()->getRoute();
                preg_match_all('/app\\\(\w+)\\\controller\\\(\w+)@(\w+)/', $rule, $ruleMatches);
                if( count( $ruleMatches ) != 4 ) {
                    throw new ApiException(ApiErrDesc::END_RULE_PARSE);
                }

                $module = 'admin';
                $controller = $ruleMatches['2'][0];
                $action = $rule[0];
            }
        }
        //
        $where = [];
        $where['app'] = $module;
        $where['controller'] = $controller;
        $where['action'] = $action;
        $where['role_id'] = array('in', implode(', ', $adminRoles));
        $res = Db::name('menu_node')
            ->alias('node')
            ->field('node.id')
            ->join('role_node rn', 'node.id = rn.node_id')
            ->where($where)
            ->find();
        if( $res ) {
            return true;

        }else{
            return false;
        }
    }
}